{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "定义测试数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "data_test=[1,1,1,1,1,2,2,2,2,3,3,3,4,4,5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算偏度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "import math\n",
    "import numpy\n",
    "def sknewness(data):\n",
    "    n = len(data) #样本个数\n",
    "    average=numpy.mean(data) #计算平均值\n",
    "    m1=0\n",
    "    m2=0\n",
    "    k=math.sqrt(n*(n-1))/(n-2)\n",
    "    for i in data:\n",
    "        m1+=(i-average)**3\n",
    "        m2+=(i-average)**2\n",
    "    m1/=n\n",
    "    m2/=n\n",
    "    m2=math.sqrt(m2**3)\n",
    "    skewness=k*m1/m2\n",
    "    return skewness"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "skewness = 0.6554279508966393\n"
     ]
    }
   ],
   "source": [
    "print('skewness =',sknewness(data_test))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
